﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.Net;
using com.lover.common;
using System.IO;
using com.lover.astd.common.model;
using com.lover.common.http;

namespace com.lover.astd.common.partner.impl
{
    public class Uz73Login : LoginImplBase
    {
        public Uz73Login() : base() { }
        public override LoginResult login(ref List<Cookie> cookies, string verify_code = null, string extra = null)
        {
            LoginResult result = new LoginResult();
            logging();
            string loginurl = "http://astd.uz73.com//user/login.do";
            string data = String.Format("userName={0}&password={1}", _username, _password);
            HttpResult resp = TransferMgr.doPostPure(loginurl, data, ref cookies);
            if (resp == null) { result.StatusCode = LoginStatusCode.FailInLogin; return result; }

            string retstr = resp.getContent();

            if (retstr.IndexOf("alert") >= 0)
            {
                result.StatusCode = LoginStatusCode.FailInLogin;
                return result;
            }

            string redirect_url = "http://astd.uz73.com//game/jumpServer.do";
            string redirect_data = String.Format("gameId=1&serverId={0}", _acc.ServerId);
            resp = TransferMgr.doPostPure(redirect_url, redirect_data, ref cookies);
            if (resp == null) { result.StatusCode = LoginStatusCode.FailInGetSession; return result; }
            string content = resp.getContent();
            Regex re = new Regex("message:'(.*?)'");
            Match m = re.Match(content);
            if (m == null || m.Groups == null || m.Groups.Count < 2) { result.StatusCode = LoginStatusCode.FailInFindingGameUrl; return result; }

            string gameurl = m.Groups[1].Value;
            processStartGame(gameurl, result, ref cookies);
            return result;
        }
    }
}
